package com.suncaper.tddmall.controller;

import com.suncaper.tddmall.common.model.Comment;
import com.suncaper.tddmall.common.model.User;
import com.suncaper.tddmall.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
public class CommontController {
    @Autowired
    CommentMapper commentMapper;

    @GetMapping("/commont")
    public ResponseEntity commont(String canshu, HttpServletRequest servletRequest) {
        //封装插入条件
        User user = (User) servletRequest.getSession().getAttribute("user");
//       if (user==null){
//           return new ResponseEntity(HttpStatus.NOT_FOUND);
//       }
        Comment comment1 = new Comment();
        Date date = new Date();
        comment1.setTime(date);
        comment1.setContent(canshu);
        comment1.setUserId(user.getUserId());
//       comment1.setUserId("test");
        commentMapper.insertSelective(comment1);
//       查询所有评论数
        List<Comment> comments = commentMapper.selectlist();
        int num = comments.size();
//        返回json
        return new ResponseEntity(num, HttpStatus.OK);
    }
}
